package com.res.demo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.res.exam.HttpConnectionManager;
import com.res.exam.OnHttpConnectionLoad;
import com.res.json.*;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class TableClassActivity<Main> extends Activity implements
		OnItemClickListener {
	JSONArray jArray;
	String result = null;
	InputStream is = null;
	StringBuilder sb = null;
	ListView tableList;

	HttpConnectionManager manager;
	List<NameValuePair> params;
	List<TableModel> list;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.table);
		tableList = (ListView) findViewById(R.id.listTable);
		manager = new HttpConnectionManager();
		params = new ArrayList<NameValuePair>();
		list = new ArrayList<TableModel>();
		tableList.setTextFilterEnabled(true);

		tableList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
			    // When clicked, show a toast with the TextView text
			    Toast.makeText(getApplicationContext(),
				((TextView) view).getText(), Toast.LENGTH_SHORT).show();
			}
		});
		manager.sentRequest("http://10.0.6.30/dip/getTable.php", params,
				new OnHttpConnectionLoad() {

					public void onHttpConnectionExecuted(String response) {
						// TODO Auto-generated method stub
						Log.d("RESPONSE", "no1 " + response);
						JSONObject a;
						try {
							a = new JSONObject(response);
							JSONArray tables = a.getJSONArray("data");
							for (int i = 0; i < tables.length(); i++) {
								JSONObject ob = tables.getJSONObject(i);
								TableModel model = new TableModel("- " + ob
										.getString("tableName") + " -");
								list.add(model);
							}
							tableList.setAdapter(new TableListAdapter(
									TableClassActivity.this, list));
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				});
		
		// tableList.setOnClickListener(this);
		tableList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	Toast.makeText(getApplicationContext(), "Haha", Toast.LENGTH_SHORT).show();
            }
    });
	}

	// menu uusgeh
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater mostrar = getMenuInflater();
		mostrar.inflate(R.menu.table_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menuProduct:
			startActivity(new Intent(this, OrderClassActivity.class));
			return true;
		case R.id.menuSetting:
			finish();
			System.exit(0);
			return true;
		}
		return false;
	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		startActivity(new Intent(this,OrderClassActivity.class));
	}
}
